/**************************************************************************//**
 * @file     eev_ctl.c
 * @brief    Electronic expansion valve control Source File
 * @version  V1.00
 * @date     30. November 2017
 *
 * @note
 *
 ******************************************************************************/

/******************************************************************************
* include file
*******************************************************************************/
#include "eev_ctl.h"

/******************************************************************************
* global data for the project
*******************************************************************************/


/******************************************************************************
* loacal data for the current file
*******************************************************************************/


/******************************************************************************/
/* local prototypes function **************************************************/
/******************************************************************************/
uint8_t mg_EEV_Statue;         // 0: Initing 1: normal work
uint8_t mg_EEV_Phase;          // 0-7: phase0 - phase7 
uint16_t mg_EEV_DesStepPoint;  // The set step point
uint16_t mg_EEV_RelStepPoint;  // Real step point
uint8_t  mg_EEV_Wait;          // EEV servo wait counter

/******************************************************************************
* loacal function for the current file
*******************************************************************************/

/******************************************************************************
* global function for the current project
*******************************************************************************/
/**
  * @brief      Electronic expansion valve setp servo. 
  *             This function should put in the suit timer(>40ms) to run.
  * @param[in]  void : None
  * @retval     void : None
  * @par        None
  * @par        Example:
  * This example shows how to call the function:
  * @code
  * mg_EEV_StepServo();
  * @endcode
  * @Note:
*/ 
void mg_EEV_StepServo(){
  if (mg_EEV_Wait < 1){
    mg_EEV_Wait ++;
    return;
  }else{
    mg_EEV_Wait = 0;
  }
    
  if (mg_EEV_DesStepPoint == mg_EEV_RelStepPoint){
    if (mg_EEV_Statue == 0) mg_EEV_Phase = 0;
    mg_EEV_Statue = 1;
  }else if (mg_EEV_DesStepPoint < mg_EEV_RelStepPoint){
    switch(mg_EEV_Phase){
      case 0:
        PHASE1;
        mg_EEV_Phase = 1;
        break;
      case 1:
        PHASE2;
        mg_EEV_Phase = 2;
        break;
      case 2:
        PHASE3;
        mg_EEV_Phase = 3;
        break;
      case 3:
        PHASE4;
        mg_EEV_Phase = 4;
        break;
      case 4:
        PHASE5;
        mg_EEV_Phase = 5;
        break;
      case 5:
        PHASE6;
        mg_EEV_Phase = 6;
        break;
      case 6:
        PHASE7;
        mg_EEV_Phase = 7;
        break;
      case 7:
        PHASE0;
        mg_EEV_Phase = 0;
        break;
      default:
        PHASE0;
        mg_EEV_Phase = 0;
        break;
    }
    mg_EEV_RelStepPoint --;
  }else{
    switch(mg_EEV_Phase){
      case 0:
        PHASE7;
        mg_EEV_Phase = 7;
        break;
      case 1:
        PHASE0;
        mg_EEV_Phase = 0;
        break;
      case 2:
        PHASE1;
        mg_EEV_Phase = 1;
        break;
      case 3:
        PHASE2;
        mg_EEV_Phase = 2;
        break;
      case 4:
        PHASE3;
        mg_EEV_Phase = 3;
        break;
      case 5:
        PHASE4;
        mg_EEV_Phase = 4;
        break;
      case 6:
        PHASE5;
        mg_EEV_Phase = 5;
        break;
      case 7:
        PHASE6;
        mg_EEV_Phase = 6;
        break;
      default:
        PHASE0;
        mg_EEV_Phase = 0;
        break;
    }
    mg_EEV_RelStepPoint ++;
  }
}

/**
  * @brief      Electronic expansion valve set step point.
  * @param[in]  uint16 : set step point value
  * @retval     void : None
  * @par        None
  * @par        Example:
  * This example shows how to call the function:
  * @code
  * mg_EEV_SetStepPoint(10);
  * @endcode
  * @Note:
*/ 
void mg_EEV_SetStepPoint(uint16_t step){
  if (mg_EEV_Statue == 1)
    mg_EEV_DesStepPoint = step;
}


/**
  * @brief      Electronic expansion valve get current step point.
  * @param[in]  void : None
  * @retval     uint16 : current real step point
  * @par        None
  * @par        Example:
  * This example shows how to call the function:
  * @code
  * mg_EEV_GetStepPoint();
  * @endcode
  * @Note:
*/ 
uint16_t mg_EEV_GetStepPoint(){
  if (mg_EEV_Statue == 0)
    return 0;
  else
    return mg_EEV_RelStepPoint;
}

/**
  * @brief     init electronic expansion valve statue.
  * @param[in]  side : 0: Set point to the zero point; 1: Set point to the max step value.
  * @retval     void : None
  * @par        None
  * @par        Example:
  * This example shows how to call the function:
  * @code
  * mg_EEV_Init(0);
  * @endcode
  * @Note:
*/ 
void mg_EEV_Init(uint8_t side){
  mg_EEV_Statue = 0;
  mg_EEV_Phase = 0;
  mg_EEV_Wait = 0;
  if (side){
    mg_EEV_RelStepPoint = 0;
    mg_EEV_DesStepPoint = MAX_STEP;
  }else{
    mg_EEV_RelStepPoint = MAX_STEP;
    mg_EEV_DesStepPoint = 0;    
  }
}

/*---------------------------------- End of the file ---------------------------------*/

